package sangpil.motionGuide.controller
{
	import fl.data.DataProvider;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.easing.*;
	
	import sangpil.motionGuide.controller.event.ControlEvent;
	import sangpil.motionGuide.controller.event.ControlReset;
	import sangpil.motionGuide.controller.event.ControlUpdate;
	import sangpil.motionGuide.controller.event.ControlUpdateAll;
	import sangpil.utils.pannel.SpSlider;
	import sangpil.utils.pannel.events.SpSliderEvent;
	
	[Event( name="resetAll", type="sangpil.motionGuide.controller.event.ControlReset")]
	[Event( name="runAll", type="sangpil.motionGuide.controller.event.ControlUpdateAll")]
	[Event( name="exportInfo", type="sangpil.motionGuide.controller.event.ControlEvent")]
	[Event( name="upDate", type="sangpil.motionGuide.controller.event.ControlUpdate")]
	public class ControllerTest extends Sprite
	{
		public function ControllerTest()
		{
			super();
			this.setLayout();
			this.setDefault();
			this.addEvent();
		}
		
		private var conBase:controllerClip;
		private var currentId:uint;
		private var slX:SpSlider = new SpSlider(-480, 480, 10, 280);
		private var slY:SpSlider = new SpSlider(0, 800, 10, 280);
		private var slScale:SpSlider = new SpSlider(0, 3, 0.1, 280, true);
		private var slRotation:SpSlider = new SpSlider(-180, 180, 1, 280);
		private var slAlpha:SpSlider = new SpSlider(0, 1, 0.05, 280, true);
		private var slDuration:SpSlider = new SpSlider(0, 3, 0.1, 280, true);
		private var slDelay:SpSlider = new SpSlider(0, 3, 0.1, 280, true);
		private var space:Number = 27;
		private var startNum:Number = 420;
		
		private var positionRect:Sprite;
		
		private function setLayout():void
		{
			var bg:Shape = new Shape();
			bg.graphics.beginFill(0x000000, 1);
			bg.graphics.drawRect(0, 0, 480, 800);
			bg.graphics.endFill();
			this.addChild(bg);
			
			this.conBase = new controllerClip();
			this.addChild(this.conBase);
			
			with(this.slX)
			{
				name = 'x';
				x = 170;
				y = startNum;
			}
			startNum += space;
			with(this.slY)
			{
				name = 'y';
				x = 170;
				y = startNum;
			}
			startNum += space;
			with(this.slScale)
			{
				name = 'scale';
				x = 170;
				y = startNum;
			}
			startNum += space;
			with(this.slRotation)
			{
				name = 'rotation';
				x = 170;
				y = startNum;
			}
			startNum += space;
			with(this.slAlpha)
			{
				name = 'alpha';
				x = 170;
				y = startNum;
			}
			startNum += space;
			with(this.slDuration)
			{
				name = 'duration';
				x = 170;
				y = startNum;
			}
			startNum += space;
			with(this.slDelay)
			{
				name = 'delay';
				x = 170;
				y = startNum;
			}
			
			this.addChild(this.slX);
			this.addChild(this.slY);
			this.addChild(this.slScale);
			this.addChild(this.slRotation);
			this.addChild(this.slAlpha);
			this.addChild(this.slDuration);
			this.addChild(this.slDelay);
			
			this.slX.addEventListener(SpSliderEvent.CHANGE_VALUE, changeVal);
			this.slY.addEventListener(SpSliderEvent.CHANGE_VALUE, changeVal);
			this.slScale.addEventListener(SpSliderEvent.CHANGE_VALUE, changeVal);
			this.slRotation.addEventListener(SpSliderEvent.CHANGE_VALUE, changeVal);
			this.slAlpha.addEventListener(SpSliderEvent.CHANGE_VALUE, changeVal);
			this.slDuration.addEventListener(SpSliderEvent.CHANGE_VALUE, changeVal);
			this.slDelay.addEventListener(SpSliderEvent.CHANGE_VALUE, changeVal);
			
			this.positionRect = new Sprite();
			this.positionRect.graphics.beginFill(0xff0000, 0.5);
			this.positionRect.graphics.drawRect(-20, -20, 40, 40);
			this.positionRect.graphics.endFill();
			this.addChild(this.positionRect);
			//this.positionRect.addEventListener(MouseEvent.MOUSE_OUT, drop);
			
			this.positionRect.x = 313;
			this.positionRect.y = 34;
			
		}
		
		private function drag(e:MouseEvent):void
		{
			e.currentTarget.addEventListener(Event.ENTER_FRAME, gogosing);
		}
		
		private function gogosing(e:Event):void
		{
			e.currentTarget.x = this.mouseX;
			e.currentTarget.y = this.mouseY;
			
			var tempX:Number = (480 - e.currentTarget.x)*-1;
			var tempY:Number = e.currentTarget.y;

			this.conBase.tbEX.text = tempX.toString();
			this.conBase.tbEY.text = tempY.toString();
			this.slX.setVal(tempX);
			this.slY.setVal(tempY);
		}
		
		private function drop(e:MouseEvent):void
		{
			e.currentTarget.removeEventListener(Event.ENTER_FRAME, gogosing);
			var dO:Object = new Object();
			dO.id = this.currentId;
			dO.name = this.conBase.tbName.text;
			dO.saved = true;
			dO.activate = false;
			dO.delay = Number(this.conBase.tbDelay.text) ;
			dO.duration = Number(this.conBase.tbDuration.text);
			dO.ease = Number(this.conBase.tbType.text);
			dO.easeData = this.conBase.cbEaseType.selectedItem.data;
			
			var sO:Object = new Object();
			sO.x = Number(this.conBase.tbSX.text);
			sO.y = Number(this.conBase.tbSY.text);
			sO.scale = 1;
			sO.rotation = 0;
			sO.alpha = 1;

			var eO:Object = new Object();
			eO.x = Number(this.conBase.tbEX.text);
			eO.y = Number(this.conBase.tbEY.text);
			eO.scale = Number(this.conBase.tbEScale.text);
			eO.rotation = Number(this.conBase.tbERotation.text);
			eO.alpha =Number(this.conBase.tbEAlpha.text);
			
			this.dispatchEvent(new ControlUpdate(dO, sO, eO, ControlUpdate.UP_DATE));
		}
		
		private function changeVal(e:SpSliderEvent):void
		{
			trace(e.value , e.currentTarget.name);
			switch(e.currentTarget.name)
			{
				case 'x':
				this.positionRect.x = 480 + e.value;
				this.conBase.tbEX.text = e.value.toString();
				break;
				case 'y':
				this.positionRect.y = e.value;
				this.conBase.tbEY.text = e.value.toString();
				break;
				case 'scale':
				this.positionRect.scaleX = this.positionRect.scaleY =  e.value;
				this.conBase.tbEScale.text = e.value.toString();
				break;
				case 'rotation':
				this.positionRect.rotation = e.value;
				this.conBase.tbERotation.text = e.value.toString();
				break;
				case 'alpha':
				this.conBase.tbEAlpha.text = e.value.toString();
				break;
				case 'duration':
				this.conBase.tbDuration.text = e.value.toString();
				break;
				case 'delay':
				this.conBase.tbDelay.text = e.value.toString();
				break;
			}
			update();
		}
		
		private function setDefault():void
		{
				this.conBase.cbEaseType.dataProvider = new DataProvider([{label:"Easing Function", data:null},
															{label:"Default (Regular.easeOut)", data:null},
															{label:"Back.easeOut", data:Back.easeOut},
															{label:"Back.easeIn", data:Back.easeIn},
															{label:"Back.easeInOut", data:Back.easeInOut},
															{label:"Bounce.easeOut", data:Bounce.easeOut},
															{label:"Bounce.easeIn", data:Bounce.easeIn},
															{label:"Bounce.easeInOut", data:Bounce.easeInOut},
															{label:"Circ.easeOut", data:Circ.easeOut},
															{label:"Circ.easeIn", data:Circ.easeIn},
															{label:"Circ.easeInOut", data:Circ.easeInOut},
															{label:"Cubic.easeOut", data:Cubic.easeOut},
															{label:"Cubic.easeIn", data:Cubic.easeIn},
															{label:"Cubic.easeInOut", data:Cubic.easeInOut},
															{label:"Elastic.easeOut", data:Elastic.easeOut},
															{label:"Elastic.easeIn", data:Elastic.easeIn},
															{label:"Elastic.easeInOut", data:Elastic.easeInOut},
															{label:"Expo.easeOut", data:Expo.easeOut},
															{label:"Expo.easeIn", data:Expo.easeIn},
															{label:"Expo.easeInOut", data:Expo.easeInOut},
															{label:"Linear.easeNone", data:Linear.easeNone},
															{label:"Quad.easeOut", data:Quad.easeOut},
															{label:"Quad.easeIn", data:Quad.easeIn},
															{label:"Quad.easeInOut", data:Quad.easeInOut},
															{label:"Quart.easeOut", data:Quart.easeOut},
															{label:"Quart.easeIn", data:Quart.easeIn},
															{label:"Quart.easeInOut", data:Quart.easeInOut},
															{label:"Quint.easeOut", data:Quint.easeOut},
															{label:"Quint.easeIn", data:Quint.easeIn},
															{label:"Quint.easeInOut", data:Quint.easeInOut},
															{label:"Sine.easeOut", data:Sine.easeOut},
															{label:"Sine.easeIn", data:Sine.easeIn},
															{label:"Sine.easeInOut", data:Sine.easeInOut}]);
							
				this.conBase.cbEaseType.selectedIndex = 0;
				this.conBase.tbType.text =  this.conBase.cbEaseType.selectedLabel;
				this.conBase.cbEaseType.addEventListener(Event.CHANGE, updateCode);
				
		}
		
		private function updateCode(e:Event):void
		{
			this.conBase.tbType.text =  this.conBase.cbEaseType.selectedLabel;
		}
		
		private function addEvent():void
		{
			this.conBase.btnReset.addEventListener(MouseEvent.CLICK, onReset);
			this.conBase.btnResetAll.addEventListener(MouseEvent.CLICK, onResetAll);
			this.conBase.btnTween.addEventListener(MouseEvent.CLICK, onTween);
			this.conBase.btnTweenAll.addEventListener(MouseEvent.CLICK, onTweenAll);
		}
		
		private function onReset(e:MouseEvent):void
		{
			trace('onReset');
			this.dispatchEvent(new ControlReset(ControlReset.RESET_ALL));
		}
		
		private function onResetAll(e:MouseEvent):void
		{
			trace('onResetAll');
			this.dispatchEvent(new ControlReset(ControlReset.RESET_ALL));
		}
		
		private function onTween(e:MouseEvent):void
		{
			trace('onTween');
			var dO:Object = new Object();
			dO.id = this.currentId;
			dO.name = this.conBase.tbName.text;
			dO.saved = true;
			dO.activate = false;
			dO.delay = Number(this.conBase.tbDelay.text) ;
			dO.duration = Number(this.conBase.tbDuration.text);
			dO.ease = Number(this.conBase.tbType.text);
			dO.easeData = this.conBase.cbEaseType.selectedItem.data;
			
			var sO:Object = new Object();
			sO.x = Number(this.conBase.tbSX.text);
			sO.y = Number(this.conBase.tbSY.text);
			sO.scale = 1;
			sO.rotation = 0;
			sO.alpha = 1;

			var eO:Object = new Object();
			eO.x = Number(this.conBase.tbEX.text);
			eO.y = Number(this.conBase.tbEY.text);
			eO.scale = Number(this.conBase.tbEScale.text);
			eO.rotation = Number(this.conBase.tbERotation.text);
			eO.alpha =Number(this.conBase.tbEAlpha.text);
				
			this.dispatchEvent(new ControlEvent(dO, sO, eO, ControlEvent.EXPORT_INFO));
		}	
		
		private function update():void
		{
			var dO:Object = new Object();
			dO.id = this.currentId;
			dO.name = this.conBase.tbName.text;
			dO.saved = true;
			dO.activate = false;
			dO.delay = Number(this.conBase.tbDelay.text) ;
			dO.duration = Number(this.conBase.tbDuration.text);
			dO.ease = Number(this.conBase.tbType.text);
			dO.easeData = this.conBase.cbEaseType.selectedItem.data;
			
			var sO:Object = new Object();
			sO.x = Number(this.conBase.tbSX.text);
			sO.y = Number(this.conBase.tbSY.text);
			sO.scale = 1;
			sO.rotation = 0;
			sO.alpha = 1;

			var eO:Object = new Object();
			eO.x = Number(this.conBase.tbEX.text);
			eO.y = Number(this.conBase.tbEY.text);
			eO.scale = Number(this.conBase.tbEScale.text);
			eO.rotation = Number(this.conBase.tbERotation.text);
			eO.alpha =Number(this.conBase.tbEAlpha.text);
			
			this.dispatchEvent(new ControlUpdate(dO, sO, eO, ControlUpdate.UP_DATE));
		}
		
		private function onTweenAll(e:MouseEvent):void
		{
			trace('onTweenAll');
			var dO:Object = new Object();
			dO.id = this.currentId;
			dO.name = this.conBase.tbName.text;
			dO.saved = true;
			dO.activate = false;
			dO.delay = Number(this.conBase.tbDelay.text) ;
			dO.duration = Number(this.conBase.tbDuration.text);
			dO.ease = Number(this.conBase.tbType.text);
			dO.easeData = this.conBase.cbEaseType.selectedItem.data;
			
			var sO:Object = new Object();
			sO.x = Number(this.conBase.tbSX.text);
			sO.y = Number(this.conBase.tbSY.text);
			sO.scale = 1;
			sO.rotation = 0;
			sO.alpha = 1;

			var eO:Object = new Object();
			eO.x = Number(this.conBase.tbEX.text);
			eO.y = Number(this.conBase.tbEY.text);
			eO.scale = Number(this.conBase.tbEScale.text);
			eO.rotation = Number(this.conBase.tbERotation.text);
			eO.alpha =Number(this.conBase.tbEAlpha.text);
			
			this.dispatchEvent(new ControlUpdateAll(dO, sO, eO, ControlUpdateAll.RUN_All));		
		}
		
		private var activate:Boolean
		public function updateInfo(dInfo:Object, sInfo:Object, eInfo:Object):void
		{
				//reset
				this.positionRect.scaleX = this.positionRect.scaleY = 1;
				this.positionRect.rotation = 0;
			
				this.currentId = dInfo.id;
				this.conBase.tbDuration.text = dInfo.duration.toString();
				this.conBase.tbDelay.text = dInfo.delay.toString();
				this.conBase.tbName.text = dInfo.name;
				this.activate = true;

 				this.conBase.tbSX.text = sInfo.x.toString();
				this.conBase.tbSY.text = sInfo.y.toString();
				this.conBase.tbSScale.text = sInfo.scale.toString();
				this.conBase.tbSRotation.text = sInfo.rotation.toString();
				this.conBase.tbSAlpha.text = sInfo.alpha.toString();
				
				
				if(eInfo.x)
				{
					this.conBase.tbEX.text = eInfo.x.toString();
					this.slX.setVal(eInfo.x);
					this.positionRect.x = eInfo.x + 480;
				}else{this.conBase.tbEX.text = '0'}
				if(eInfo.y)
				{
					this.conBase.tbEY.text = eInfo.y.toString();
					this.slY.setVal(eInfo.y);
					this.positionRect.y = eInfo.y;
				}else{this.conBase.tbEY.text = '0'}
				if(eInfo.alpha)
				{
					this.conBase.tbEAlpha.text = eInfo.alpha.toString();
					this.slAlpha.setVal(eInfo.alpha);
				}else{this.conBase.tbEAlpha.text = '0'}
				if(eInfo.scale)
				{
					this.conBase.tbEScale.text = eInfo.scale.toString();
					this.slScale.setVal(eInfo.scale);
					this.positionRect.scaleX = this.positionRect.scaleY = eInfo.scale;
				}else{this.conBase.tbEScale.text = '0'}
				if(eInfo.rotation)
				{
					this.positionRect.rotation = eInfo.rotation;
					this.conBase.tbERotation.text = eInfo.rotation.toString();
					this.slRotation.setVal(eInfo.rotation);
				}else{this.conBase.tbERotation.text = '0'}
				
/*								
				this.conBase.tbEX.text = eInfo.x.toString();
				this.conBase.tbEY.text = eInfo.y.toString();
				this.conBase.tbEScale.text = eInfo.scale.toString();
				this.conBase.tbERotation.text = eInfo.rotation.toString();
				this.conBase.tbEAlpha.text = eInfo.alpha.toString();
				this.conBase.tbERotation.text = eInfo.rotation.toString();
				 */
				 
			this.positionRect.addEventListener(MouseEvent.MOUSE_DOWN, drag);
			this.positionRect.addEventListener(MouseEvent.MOUSE_UP, drop);
		}
		
	}
}